package com.itheima.reggie.controller;


import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    //查询所有
    @GetMapping("/category/findAll")
    public ResultInfo findAll(){
        //调用categoryService 方法查找所有
        List<Category> categoryList = categoryService.findAll();
        //返回ResultInfo存储好的所有数据的对象
        return ResultInfo.success(categoryList);
    }


    //添加对象
    @PostMapping("/category")
    public ResultInfo addCategory(@RequestBody Category category){
        //调用categoryService保存对象方法
        categoryService.addCategory(category);

        //保存成功，返回ResultInfo对象
        return ResultInfo.success(null);
    }

    //更新对象
    @PutMapping("/category")
    public ResultInfo updateCategory(@RequestBody Category category){
        System.out.println("1");
        //调用categoryService更新对象方法
        categoryService.updateCategory(category);

        //保存成功，返回ResultInfo对象
        return ResultInfo.success(null);
    }

    //通过id删除对象
    @DeleteMapping("/category")
    public ResultInfo deleteCategory(Long id){
        //调用categoryService删除对象方法,返回ResultInfo对象
        return categoryService.deleteCateGory(id);
    }

    //获取菜品分类
    @GetMapping("/category/list")
    public ResultInfo findCategoryList(Integer type){
        List<Category> categoryList = categoryService.findCategoryList(type);
        return ResultInfo.success(categoryList);
    }
}
